﻿#region Usings
using System;
using System.Collections.Generic;

using PolyGameEngine.GamePieces;
using PolyGameEngine.GamePieces.Common;
using PolyGameEngine.Tools;
using PolyGameEngine.Debug;
using PolyGameEngine.LevelTools;
using PolyMorph.CustomGamePieces;

using FarseerPhysics.DebugViews;
using FarseerPhysics.Dynamics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

#endregion



namespace PolyMorph.LevelEditor
{ 
    public class Level : DrawableGameComponent
    {
        public static Level Singleton;
        #region Fields
        Border border;
        public bool BuildBorder = false;
        public bool editorMode;
        bool WaitingForTilePlacement = false;
        Dictionary<string, Material> materials = new Dictionary<string, Material>();
        Dictionary<TileName, Texture2D> Textures = new Dictionary<TileName, Texture2D>();
        float rot = 0;
        float rpm = 0;
        GamePiece UNITGP;
        int GPindex = 0;
        int BGindex = 0;
        int SaveLineCounter = 0;
        static List<GamePiece> gamePieces = new List<GamePiece>();
        static List<string> stringList = new List<string>();
        PhysicsGame physicsGame;
        SpriteBatch sb;
        SpriteFont font;
        public static string currentLevel = "Level0";
        static TileName currentSelectedTile;
        List<Texture2D> Backgrounds = new List<Texture2D>();
        static Vector2 StartPos = new Vector2(46, 46);
        static Vector2 gravity;
        World world;
        Vector2 Gravity
        {
            set
            {
                gravity = value;
                world.Gravity = value;
            }
            get { return world.Gravity; }
        }
        float GravityX
        {
            set
            {
                gravity.X = value;
                world.Gravity.X = value;
            }
            get { return world.Gravity.X; }
        }
        float GravityY
        {
            set
            {
                gravity.Y = value;
                world.Gravity.Y = value;
            }
            get { return world.Gravity.Y; }
        }
        public static Vector2 levelSize;
        static Vector2 tempStartPosition;
        #endregion

        #region Properties
        public ContentManager Content
        {
            get { return physicsGame.Content; }
        }
        public GameComponentCollection Components
        {
            get { return physicsGame.Components; }
        }
        public Material DefaultMaterial
        {
            get
            {
                return materials["Default"];
            }
            set
            {
                materials["Default"] = value;
            }
        }
        public Texture2D DefaultTexture
        {
            get
            {
                return materials["Default"].Texture;
            }
        }

        public bool EditorMode
        {
            get { return editorMode; }
            set { editorMode = value; }
        }

        public GraphicsDeviceManager graphics
        {
            get { return physicsGame.graphics; }
        }
        private Vector2 mouseposition
        {
            get
            {
                if (InputMan.IsKeyDown(Keys.LeftControl))
                {
                    return InputMan.MousePosition;
                }
                else
                    return InputMan.MousePositionGrid;
            }
        }
        public Vector2 Offset
        {
            get
            {
                return GamePiece._ScreenOffset;
            }
        }
        public float Yoffset
        {
            get { return GamePiece._ScreenOffset.Y; }
            set { GamePiece._ScreenOffset.Y = value; }
        }
        public float Xoffset
        {
            get { return GamePiece._ScreenOffset.X; }
            set { GamePiece._ScreenOffset.X = value; }
        }
        public Vector2 PlayerPosition
        {
            get
            {
                return gamePieces[0].position;
            }
            
        }
        public Vector2 PlayerVelocity
        {
            get
            {
                    return gamePieces[0].Velocity;
            }
        }
        public GamePiece PreviouslyAddedGamePiece
        {
            get
            {
                return gamePieces[0];
            }
        }
        public float rotation
        {
            get { return MathHelper.ToDegrees(rot); }
            set { rot = MathHelper.ToRadians(value); }
        }
        public Vector2 ScreenMoveLimitSize
        {
            get
            {
                return new Vector2(250, 250);
            }
        }
        public Vector2 viewport
        {
            get
            {
                return new Vector2(physicsGame.graphics.GraphicsDevice.Viewport.Width, physicsGame.graphics.GraphicsDevice.Viewport.Height);
            }
        }
        #endregion

        #region Constructor
        public Level(PhysicsGame PhysicsGame)
            : base(PhysicsGame)
        {
            Singleton = this;
            physicsGame = PhysicsGame;
            world = new World(Vector2.Zero);

            font = Content.Load<SpriteFont>(@"Fonts\font");
            sb = physicsGame.spriteBatch;
            LoadTextures();
            AddMaterials();
            Components.Add(this);
            DebugStuff();
        }
        #endregion

        #region Functions

        public GamePiece addGamePiece(GamePiece GamePiece)
        {
            GamePiece.Initialize();
            if (!GamePiece.GetType().IsSubclassOf(typeof(Unit)))
                Components.Add(GamePiece);
            gamePieces.Add(GamePiece);
            SaveLineCounter++;
            return GamePiece;
        }
        public void addMaterial(Material Material)
        {
            materials.Add(Material.Identifier, Material);

        }
        public void AddMaterials()
        {
            addMaterial(new Material(Content.Load<Texture2D>("Materials//Stone"), "Default", 0.8f, 0.2f));
            addMaterial(new Material(loadMaterial("Wood"), "Wood",0.8f,0.5f));
            addMaterial(new Material(loadMaterial("Steel"), "Steel",0.8f,0.2f));
            addMaterial(new Material(loadMaterial("Rubber"), "Rubber",1,0.95f));
            addMaterial(new Material(loadMaterial("Plastic"), "Plastic",0.6f,0.7f));
        }
        public void BuildBorderLoop()
        {
            GamePiece._ScreenOffset = Vector2.Zero;
            if (InputMan.IsKeyDown(Keys.Right))
            {
                levelSize.X += 50;
            }
            if (InputMan.IsKeyDown(Keys.Down))
            {
                levelSize.Y += 50;
            }
            if (InputMan.IsKeyDown(Keys.Left))
            {
                levelSize.X -= 50;
            }
            if (InputMan.IsKeyDown(Keys.Up))
            {
                levelSize.Y -= 50;
            }
            if (levelSize.X > 3900)
                levelSize.X = 3900;
            if (levelSize.Y > 3900)
                levelSize.Y = 3900;
            if (levelSize.X <= 50)
                levelSize.X = 50;
            if (levelSize.Y <= 50)
                levelSize.Y = 50;
            if (InputMan.OnPress(Keys.Enter))
            {
                BuildBorder = false;
                border = new Border(physicsGame, world, levelSize, DefaultTexture);
                Components.Add(border);
            }


        }
        public void BuildCurrentGamePiece()
        {
            Vector2 LevelPosition = (mouseposition - Offset);

            if (TileName.TeleportIn == currentSelectedTile || TileName.TeleportOut == currentSelectedTile)
            {
                BuilTeleport(LevelPosition);
            }

            if (!WaitingForTilePlacement)
            {
                switch (currentSelectedTile)
                {
                    case TileName.BigBoulder:
                        addGamePiece(new RoundRobin(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, rpm, 100, rotation, SaveLineCounter, "BigBoulder"));
                        stringList.Add("@RoundRobin BoulderBig " + LevelPosition.X + " " + LevelPosition.Y + " " + rpm + " 100 " + rotation + " " + SaveLineCounter + " BigBoulder");
                        break;
                    case TileName.MediumBoulder:
                        addGamePiece(new RoundRobin(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, rpm, 100, rotation, SaveLineCounter, "MediumBoulder"));
                        stringList.Add("@RoundRobin BoulderMedium " + LevelPosition.X + " " + LevelPosition.Y + " " + rpm + " 100 " + rotation + " " + SaveLineCounter + " MediumBoulder");
                        break;
                    case TileName.SmallBoulder:
                        addGamePiece(new RoundRobin(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, rpm , 100, rotation, SaveLineCounter, "SmallBoulder"));
                        stringList.Add("@RoundRobin BoulderSmall " + LevelPosition.X + " " + LevelPosition.Y + " " + rpm +  " 100 " + rotation + " " + SaveLineCounter + " SmallBoulder");
                        break;
                    case TileName.CrossPlank:
                        addGamePiece(new RoundRobin(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, rpm, 100, rotation, SaveLineCounter, "CrossPlank"));
                        stringList.Add("@RoundRobin CrossPlank " + LevelPosition.X + " " + LevelPosition.Y + " " + rpm + " 100 " + rotation + " " + SaveLineCounter + " CrossPlank");
                        break;
                    case TileName.Fan:
                        addGamePiece(new Fan(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, rotation, SaveLineCounter));
                        stringList.Add("@Fan FanSprite " + LevelPosition.X + " " + LevelPosition.Y + " " + rotation + " " + SaveLineCounter);
                        break;
                    case TileName.Lava:
                        addGamePiece(new Lava(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, SaveLineCounter));//Not Rotate
                        stringList.Add("@Lava LavaSprite " + LevelPosition.X + " " + LevelPosition.Y + " " + SaveLineCounter);
                        break;
                    case TileName.Magnet:
                        addGamePiece(new Magnet(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, rotation, SaveLineCounter));
                        stringList.Add("@Magnet Magnet " + LevelPosition.X + " " + LevelPosition.Y + " " + rotation + " " + SaveLineCounter);
                        break;
                    case TileName.Oil:
                        addGamePiece(new Oil(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, SaveLineCounter));//Not Rotate
                        stringList.Add("@Oil OilSprite " + LevelPosition.X + " " + LevelPosition.Y + " " + SaveLineCounter);
                        break;
                    case TileName.Plank:
                        addGamePiece(new RoundRobin(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, rpm, 5, rotation, SaveLineCounter, "Plank"));
                        stringList.Add("@RoundRobin Plank " + LevelPosition.X + " " + LevelPosition.Y + " " + rpm + " 1 5 " + rotation + " " + SaveLineCounter + " Plank");
                        break;
                    case TileName.Plastic:
                        addGamePiece(new MaterialTile(physicsGame, world, materials["Plastic"], LevelPosition, rotation, SaveLineCounter, "Plastic"));//Not Rotate
                        stringList.Add("@MaterialTile Plastic " + LevelPosition.X + " " + LevelPosition.Y + " " + rotation + " " + SaveLineCounter + " Plastic");
                        break;
                    case TileName.Quarterpipe:
                        addGamePiece(new SmallPieceFromTexture(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, rotation, SaveLineCounter, "QuarterPipe"));
                        stringList.Add("@SmallPieceFromTexture QuarterPipe " + LevelPosition.X + " " + LevelPosition.Y + " " + rotation + " " + SaveLineCounter + " QuarterPipe");
                        break;
                    case TileName.Quicksand:
                        addGamePiece(new QuickSand(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, SaveLineCounter, 1));//Not Rotate
                        stringList.Add("@QuickSand QuicksandSprite " + LevelPosition.X + " " + LevelPosition.Y + " " + SaveLineCounter + " 1");
                        break;
                    case TileName.Rubber:
                        addGamePiece(new MaterialTile(physicsGame, world, materials["Rubber"], LevelPosition, rotation, SaveLineCounter, "Rubber"));//Not Rotate
                        stringList.Add("@MaterialTile Rubber " + LevelPosition.X + " " + LevelPosition.Y + " " + rotation + " " + SaveLineCounter + " Rubber");
                        break;
                    case TileName.Spike:
                        addGamePiece(new SmallPieceFromTexture(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, rotation, SaveLineCounter, "Spike"));
                        stringList.Add("@SmallPieceFromTexture Spike " + LevelPosition.X + " " + LevelPosition.Y + " " + rotation + " " + SaveLineCounter + " Spike");
                        break;
                    case TileName.SpikeyRobin:
                        addGamePiece(new RoundRobin(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, rpm , 100, rotation, SaveLineCounter, "SpikeyRobin"));
                        stringList.Add("@RoundRobin SpikeyRobin " + LevelPosition.X + " " + LevelPosition.Y + " " + rpm  + " 100 " + rotation + " " + SaveLineCounter + " Spikeyrobin");
                        break;
                    case TileName.Steel:
                        addGamePiece(new MaterialTile(physicsGame, world, materials["Steel"], LevelPosition, rotation, SaveLineCounter, "Steel"));
                        stringList.Add("@MaterialTile Steel " + LevelPosition.X + " " + LevelPosition.Y + " " + rotation + " " + SaveLineCounter + " Steel");
                        break;
                    case TileName.Stone:
                        addGamePiece(new Tile(physicsGame, world, DefaultMaterial, LevelPosition, rotation, SaveLineCounter, "Stone"));
                        stringList.Add("@Tile Stone " + LevelPosition.X + " " + LevelPosition.Y + " " + rotation + " " + SaveLineCounter + " Stone");
                        break;
                    case TileName.Vacuum:
                        addGamePiece(new Vacuum(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, rotation, SaveLineCounter));
                        stringList.Add("@Vacuum Vacuum " + LevelPosition.X + " " + LevelPosition.Y + " " + rotation + " " + SaveLineCounter);
                        break;
                    case TileName.Water:
                        addGamePiece(new Water(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, SaveLineCounter));
                        stringList.Add("@Water WaterSprite " + LevelPosition.X + " " + LevelPosition.Y + " " + SaveLineCounter);
                        break;
                    case TileName.Wood:
                        addGamePiece(new MaterialTile(physicsGame, world, materials["Wood"], LevelPosition, rotation, SaveLineCounter, "Wood"));
                        stringList.Add("@MaterialTile Wood " + LevelPosition.X + " " + LevelPosition.Y + " " + rotation + " " + SaveLineCounter + " Wood");
                        break;
                    case TileName.Fin:
                        addGamePiece(new Finnish(physicsGame, world, DefaultMaterial, Textures[currentSelectedTile], LevelPosition, SaveLineCounter));
                        stringList.Add("@Finnish Fin " + LevelPosition.X + " " + LevelPosition.Y + " " + SaveLineCounter);
                        break;
                    case TileName.Start:
                        StartPos = LevelPosition;
                        break;
                    case TileName.GravityShift:
                        if ((int)MathHelper.ToDegrees(Math.Abs(MathHelper.WrapAngle(rotation))) % 5 == 0)
                        {
                            addGamePiece(new GravityShifTile(physicsGame, world, DefaultMaterial, LevelPosition, 0, SaveLineCounter, Textures[TileName.GravityShift]));
                            stringList.Add("@GravityShifTile GravityShift " + LevelPosition.X + " " + LevelPosition.Y + " 0 " + SaveLineCounter);
                        }
                        else
                        {
                            addGamePiece(new GravityShifTile(physicsGame, world, DefaultMaterial, LevelPosition, 180, SaveLineCounter, Textures[TileName.GravityShift]));
                            stringList.Add("@GravityShifTile GravityShift " + LevelPosition.X + " " + LevelPosition.Y + " 180 " + SaveLineCounter);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        public void BuilTeleport(Vector2 LevelPosition)
        {
            if (currentSelectedTile == TileName.TeleportOut)
            {
                addGamePiece(new Teleporter(physicsGame, world, DefaultMaterial, LoadTexture("TPinSprite"), LoadTexture("TPoutSprite"), tempStartPosition, LevelPosition, SaveLineCounter));
                stringList.Add("@Teleporter " + tempStartPosition.X + " " + tempStartPosition.Y + " " + LevelPosition.X + " " + LevelPosition.Y + " " + SaveLineCounter);
                WaitingForTilePlacement = false;
                currentSelectedTile = TileName.TeleportIn;
                return;
            }

            if (TileName.TeleportIn == currentSelectedTile)
            {
                WaitingForTilePlacement = true;
                tempStartPosition = LevelPosition;
                currentSelectedTile = TileName.TeleportOut;
            }
 
        }
        public void BuildLevel(string Data)
        {
            Reset();
            string[] LineArr = Data.Split('\n');
            foreach (string x in LineArr)
            {
                if (x != "\r")
                    stringList.Add(x);
            }

            for (int i = 0; i < LineArr.Length; i++)
            {
                GamePiece temp = BuildLevelLine(LineArr[i]);
                if (temp != null)
                    Components.Add(temp);
            }
        }
        public GamePiece BuildLevelLine(string line)
        {
            string[] arr = line.Split(' ');
            if(arr[0].Equals("@LevelSettings"))
            {
                Gravity =  new Vector2(Convert.ToSingle(arr[1]), Convert.ToSingle(arr[2]));
                BGindex = Convert.ToInt32(arr[5]);
                
                levelSize = new Vector2(Convert.ToSingle(arr[6]), Convert.ToSingle(arr[7]));
                border = new Border(physicsGame, world, levelSize, DefaultTexture);
                Components.Add(border);
                physicsGame.Background = Backgrounds[BGindex];
                if(editorMode)
                    UNITGP = new CircleUnit(physicsGame, world, materials["Wood"], new Vector2(Convert.ToSingle(arr[3]), Convert.ToSingle(arr[4])), 0, 22, 0.5f, 0, "Player");//#######################################################
                else
                return addGamePiece(new CircleUnit(physicsGame, world, materials["Wood"], new Vector2(Convert.ToSingle(arr[3]), Convert.ToSingle(arr[4])), 0, 22, 0.5f, 0, "Player"));
            }
            if (arr[0].Equals("@Material"))//@ vir Material
            {
                addMaterial(getMaterial(arr[1], arr[2]));
            }

            if (arr[0].Equals("@RoundRobin"))
            {
                addGamePiece(new RoundRobin(physicsGame, world, DefaultMaterial,
                    LoadTextureGP(arr[1]), new Vector2(Convert.ToSingle(arr[2]),
                        Convert.ToSingle(arr[3])), Convert.ToSingle(arr[4]), Convert.ToSingle(arr[5]), Convert.ToSingle(arr[6]), Convert.ToInt32(arr[7]), arr[8]));
            }

            if (arr[0].Equals("@Fan"))
            {
                addGamePiece(new Fan(physicsGame, world, DefaultMaterial,
                    LoadTexture(arr[1]), new Vector2(Convert.ToSingle(arr[2]),
                        Convert.ToSingle(arr[3])), Convert.ToSingle(arr[4]), Convert.ToInt32(arr[5])));
            }

            if (arr[0].Equals("@Lava"))
            {
                addGamePiece(new Lava(physicsGame, world, DefaultMaterial,
                    LoadTexture(arr[1]), new Vector2(Convert.ToSingle(arr[2]),
                        Convert.ToSingle(arr[3])), Convert.ToInt32(arr[4])));
            }

            if (arr[0].Equals("@Magnet"))
            {
                addGamePiece(new Magnet(physicsGame, world, DefaultMaterial,
                    LoadTexture(arr[1]), new Vector2(Convert.ToSingle(arr[2]),
                        Convert.ToSingle(arr[3])), Convert.ToSingle(arr[4]), Convert.ToInt32(arr[5])));
            }

            if (arr[0].Equals("@Oil"))
            {
                addGamePiece(new Oil(physicsGame, world, DefaultMaterial,
                    LoadTexture(arr[1]), new Vector2(Convert.ToSingle(arr[2]),
                        Convert.ToSingle(arr[3])), Convert.ToInt32(arr[4])));
            }

            if (arr[0].Equals("@MaterialTile"))
            {
                addGamePiece(new MaterialTile(physicsGame, world, arr[1].Equals("Stone") ? DefaultMaterial : materials[arr[1]],
                    new Vector2(Convert.ToSingle(arr[2]), Convert.ToSingle(arr[3])),
                    Convert.ToSingle(arr[4]), Convert.ToInt32(arr[5]), arr[6]));
            }

            if (arr[0].Equals("@SmallPieceFromTexture"))
            {
                addGamePiece(new SmallPieceFromTexture(physicsGame, world, DefaultMaterial,
                     LoadTextureGP(arr[1]), new Vector2(Convert.ToSingle(arr[2]),
                         Convert.ToSingle(arr[3])), Convert.ToSingle(arr[4]), Convert.ToInt32(arr[5]), arr[6]));
            }

            if (arr[0].Equals("@QuickSand"))
            {
                addGamePiece(new QuickSand(physicsGame, world, DefaultMaterial,
                    LoadTexture(arr[1]), new Vector2(Convert.ToSingle(arr[2]),
                        Convert.ToSingle(arr[3])), Convert.ToInt32(arr[4]), Convert.ToSingle(arr[4])));
            }

            if (arr[0].Equals("@Tile"))
            {
                addGamePiece(new Tile(physicsGame, world, arr[1].Equals("Stone") ? DefaultMaterial : materials[arr[1]],
                    new Vector2(Convert.ToSingle(arr[2]), Convert.ToSingle(arr[3])),
                    Convert.ToSingle(arr[4]), Convert.ToInt32(arr[5]), arr[6]));
            }

            if (arr[0].Equals("@Vacuum"))
            {
                addGamePiece(new Vacuum(physicsGame, world, DefaultMaterial,
                    LoadTexture(arr[1]), new Vector2(Convert.ToSingle(arr[2]),
                        Convert.ToSingle(arr[3])), Convert.ToSingle(arr[4]), Convert.ToInt32(arr[5])));
            }

            if (arr[0].Equals("@Water"))
            {
                addGamePiece(new Water(physicsGame, world, DefaultMaterial,
                    LoadTexture(arr[1]), new Vector2(Convert.ToSingle(arr[2]),
                        Convert.ToSingle(arr[3])), Convert.ToInt32(arr[4])));
            }

            if (arr[0].Equals("@Finnish"))
            {
                addGamePiece(new Finnish(physicsGame, world, DefaultMaterial, LoadTexture(arr[1]),
                    new Vector2(Convert.ToSingle(arr[2]),
                        Convert.ToSingle(arr[3])), Convert.ToInt32(arr[4])));
            }
            if (arr[0].Equals("@GravityShifTile"))
            {
                addGamePiece(new GravityShifTile(physicsGame, world, DefaultMaterial, new Vector2(Convert.ToSingle(arr[2]), Convert.ToSingle(arr[3])), Convert.ToSingle(arr[4]), Convert.ToInt32(arr[5]), LoadTexture(arr[1])));
            }
            if (arr[0].Equals("@Teleporter"))
            {
                addGamePiece(new Teleporter(physicsGame, world, DefaultMaterial, Textures[TileName.TeleportIn], Textures[TileName.TeleportOut],
                    new Vector2(Convert.ToSingle(arr[1]), Convert.ToSingle(arr[2])),
                    new Vector2(Convert.ToSingle(arr[3]), Convert.ToSingle(arr[4])), Convert.ToInt32(arr[5])));
            }
            return null;
        }
        public void CycleGamePieceIndex(int X)
        {
                GPindex += X;
                if (GPindex== 24)
                    GPindex = 0;
                if (GPindex < 0)
                    GPindex = 23;
            
        }
        public void CycleBackgroundIndex(int X)
        {
            BGindex += X;
            if (BGindex == 11)
                BGindex = 0;
            if (BGindex < 0)
                BGindex = 10;
            physicsGame.Background = Backgrounds[BGindex];
        }
        public void DeleteGamePiece(Vector2 Position)
        {
            GamePiece toDelete;
            Fixture found = world.TestPoint(ConvertUnits.ToSimUnits(Position));
            if (found != null && found.UserData != null)
            {
                toDelete = (GamePiece)(found.UserData);
                world.RemoveBody(toDelete.body);
                stringList[toDelete.SaveLineIndex] = "";
                gamePieces.Remove(toDelete);
                Components.Remove(toDelete);
            }

        }
        public void DrawStartPos()
        {
            if (editorMode)
                sb.Draw(Textures[TileName.Start], StartPos - new Vector2(25, 25) + GamePiece._ScreenOffset, Color.White);
        }
        public void DrawArrow(Rectangle MRECT)
        {
            if (rpm>0)
                sb.Draw(Textures[TileName.ClockwiseArrow], MRECT, new Rectangle(0, 0, 150, 150), Color.White, 0, new Vector2(75, 75), SpriteEffects.None, 1);
            else if(rpm<0)
                sb.Draw(Textures[TileName.AntiClockwiseArrow], MRECT, new Rectangle(0, 0, 150, 150), Color.White, 0, new Vector2(75, 75), SpriteEffects.None, 1);

            sb.DrawString(font, "RPM: " + rpm, mouseposition-new Vector2(15,10), Color.White);
        }
        public void DrawDecoyImage()
        {
            Rectangle Rect5050 = new Rectangle(0, 0, 50, 50);
            Rectangle Rect100100 = new Rectangle(0, 0, 100, 100);
            Rectangle Rect150150 = new Rectangle(0, 0, 150, 150);
            Rectangle Rect200200 = new Rectangle(0, 0, 200, 200);
            Rectangle MRect5050 = new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 50, 50);
            Rectangle MRect100100 = new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 100, 100);
            Rectangle MRect150150 = new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 150, 150);
            Rectangle MRect200200 = new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 200, 200);
            Rectangle OriginRect = new Rectangle(1, 1, 2, 2);
            if (TileName.TeleportIn == currentSelectedTile)
            {
                sb.Draw(Textures[TileName.TeleportIn], MRect5050, Rect5050, Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 1);
            }
            if (TileName.TeleportOut == currentSelectedTile)
            {
                sb.Draw(Textures[TileName.TeleportIn], new Rectangle((int)(tempStartPosition.X + GamePiece._ScreenOffset.X), (int)(tempStartPosition.Y + GamePiece._ScreenOffset.Y), 50, 50), Rect5050, Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 1);
                sb.Draw(Textures[TileName.TeleportOut], MRect5050, Rect5050, Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 1);
            }

            if(!WaitingForTilePlacement)
            {
                switch (currentSelectedTile)
                {
                    case TileName.BigBoulder:
                        sb.Draw(Textures[currentSelectedTile], MRect150150, Rect150150, Color.White, rot, new Vector2(75, 75), SpriteEffects.None, 1);
                        DrawArrow(MRect150150);
                        break;
                    case TileName.MediumBoulder:
                        sb.Draw(Textures[currentSelectedTile], MRect100100, Rect100100, Color.White, rot, new Vector2(50, 50), SpriteEffects.None, 1);
                        DrawArrow(MRect150150);
                        break;
                    case TileName.SmallBoulder:
                        sb.Draw(Textures[currentSelectedTile], MRect5050, Rect5050, Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 1);
                        DrawArrow(MRect150150);
                        break;
                    case TileName.CrossPlank:
                        sb.Draw(Textures[currentSelectedTile], MRect200200, Rect200200, Color.White, rot, new Vector2(100, 100), SpriteEffects.None, 0.1f);
                        DrawArrow(MRect150150);
                        break;
                    case TileName.Fan:
                        sb.Draw(Textures[currentSelectedTile], MRect5050, Rect5050, Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Lava:
                        sb.Draw(Textures[currentSelectedTile], MRect5050, Rect5050, Color.White, 0, new Vector2(25, 25), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Magnet:
                        sb.Draw(Textures[currentSelectedTile], MRect5050, Rect5050, Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Oil:
                        sb.Draw(Textures[currentSelectedTile], MRect5050, Rect5050, Color.White, 0, new Vector2(25, 25), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Plank:
                        sb.Draw(Textures[currentSelectedTile], new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 200, 22), new Rectangle(0, 0, 200, 22), Color.White, rot, new Vector2(100, 11), SpriteEffects.None, 0.1f);
                        DrawArrow(MRect150150);
                        break;
                    case TileName.Plastic:
                        sb.Draw(Textures[currentSelectedTile], new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 50, 50), new Rectangle(0, 0, 50, 50), Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Quarterpipe:
                        sb.Draw(Textures[currentSelectedTile], MRect5050, Rect5050, Color.White, rot, new Vector2(12.47287f, 38.24838f), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Quicksand:
                        sb.Draw(Textures[currentSelectedTile], MRect5050, Rect5050, Color.White, 0, new Vector2(25, 25), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Rubber:
                        sb.Draw(Textures[currentSelectedTile], new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 50, 50), new Rectangle(0, 0, 50, 50), Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Spike:
                        sb.Draw(Textures[currentSelectedTile], new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 16, 19), new Rectangle(0, 0, 16, 19), Color.White, rot, new Vector2(8, 9.5f), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.SpikeyRobin:
                        sb.Draw(Textures[currentSelectedTile], new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 100, 100), new Rectangle(0, 0, 100, 100), Color.White, rot, new Vector2(50, 50), SpriteEffects.None, 0.1f);
                        DrawArrow(MRect150150);
                        break;
                    case TileName.Steel:
                        sb.Draw(Textures[currentSelectedTile], new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 50, 50), new Rectangle(0, 0, 50, 50), Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Stone:
                        sb.Draw(Textures[currentSelectedTile], MRect5050, Rect5050, Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Vacuum:
                        sb.Draw(Textures[currentSelectedTile], MRect5050, Rect5050, Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Water:
                        sb.Draw(Textures[currentSelectedTile], MRect5050, Rect5050, Color.White, 0, new Vector2(25, 25), SpriteEffects.None, 0.1f);
                        break;
                    case TileName.Wood:
                        sb.Draw(Textures[currentSelectedTile], new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 50, 50), new Rectangle(0, 0, 50, 50), Color.White, rot, new Vector2(25, 25), SpriteEffects.None, 1);
                        break;
                    case TileName.Fin:
                        sb.Draw(Textures[currentSelectedTile], new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 50, 50), new Rectangle(0, 0, 50, 50), Color.White, 0, new Vector2(25, 25), SpriteEffects.None, 1);
                        break;
                    case TileName.Start:
                        sb.Draw(Textures[currentSelectedTile], new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 50, 50), new Rectangle(0, 0, 50, 50), Color.White, 0, new Vector2(25, 25), SpriteEffects.None, 1);
                        break;
                    case TileName.GravityShift:
                        if ((int)MathHelper.ToDegrees(Math.Abs(MathHelper.WrapAngle(rotation)))%5==0)
                            sb.Draw(Textures[currentSelectedTile], new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 50, 50), new Rectangle(0, 0, 50, 50), Color.White, 0, new Vector2(25, 25), SpriteEffects.None, 1);
                        else
                            sb.Draw(Textures[currentSelectedTile], new Rectangle((int)mouseposition.X, (int)mouseposition.Y, 50, 50), new Rectangle(0, 0, 50, 50), Color.White, 3.14f, new Vector2(25, 25), SpriteEffects.None, 1);
                        break;
                    default:
                        break;
                }
            }
        }
        public Material getMaterial(string name, string Identifier)
        {
            return new Material(loadMaterial(name), Identifier);
        }
        public void HandleInput()
        {
            //LeftAlt - Change Round Robin Rotation Direction
            //MiddleMouse - Clear RPM
            //num5 - Cycle Backgrounds
            //Left/Right - Selection change
            //F1 - Toggle Editor/Play mode
            //F5 - Save Level
            //F6 - Load Level
            //F12 - Clear Level

            if (InputMan.OnPress(Keys.F1)&&PhysicsGame.gamestate == GameState.Edit)
                ToggleEditorMode();
            if (GameState.Edit == PhysicsGame.gamestate)
            {
                if (InputMan.OnPress(Keys.NumPad7))
                    CycleBackgroundIndex(-1);
                if (InputMan.OnPress(Keys.NumPad9))
                    CycleBackgroundIndex(1);
            }
                

            if (editorMode&&!BuildBorder)
            {
                if (InputMan.onLeftMousePress())
                    BuildCurrentGamePiece();
                if (!WaitingForTilePlacement)
                {
                    if (InputMan.OnRelease(Keys.F5))
                        Save();
                    if (InputMan.OnRelease(Keys.F6))
                        Load(currentLevel);
                    if (InputMan.OnPress(Keys.F12))
                        Reset();
                    if (InputMan.onMiddleMousePress())
                        rpm = 0;
                    if (InputMan.onRightMousePress())
                        DeleteGamePiece(mouseposition - Offset);
                    if (InputMan.OnPress(Keys.LeftAlt))
                    {
                        rpm = -rpm;
                    }
                    if (InputMan.OnPress(Keys.NumPad7))
                        CycleBackgroundIndex(-1);
                    if (InputMan.OnPress(Keys.NumPad9))
                        CycleBackgroundIndex(1);
                    if (InputMan.IsKeyDown(Keys.LeftControl) || InputMan.IsKeyDown(Keys.RightControl))
                    {
                        if (InputMan.IsKeyDown(Keys.Up))
                        {
                            if (GravityY > -5 && GravityY < 5)
                            {
                                GravityY = GravityY - 0.1f;
                                gravity = Gravity;
                            }
                            else
                            {
                                if (GravityY < -5)
                                    GravityY = -5;
                                if (GravityY > 5)
                                    GravityY = 5;
                            }
                        }
                        if (InputMan.IsKeyDown(Keys.Down))
                        {
                            if (GravityY >= -5 && GravityY <= 5)
                            {
                                GravityY = GravityY + 0.1f;
                                gravity = Gravity;
                            }
                            else
                            {
                                if (GravityY < -5)
                                    GravityY = -5;
                                if (GravityY > 5)
                                    GravityY = 5;
                            }
                        }
                        if (InputMan.IsKeyDown(Keys.NumPad5))
                        {
                            Gravity = new Vector2(0, 4); ;
                        }
                    }
                    else
                    {
                        if (InputMan.OnPress(Keys.Right))
                            CycleGamePieceIndex(1);
                        if (InputMan.OnPress(Keys.Left))
                            CycleGamePieceIndex(-1);
                    }
                }
                
            }
        }
        public void Save()
        {
            string temp = "";
            temp += "@LevelSettings " + GravityX + " " + GravityY + " " + StartPos.X + " " + StartPos.Y + " " + BGindex + " " + levelSize.X + " " + levelSize.Y + "\n";
            foreach (string x in stringList)
            {
                temp += x + "\n";
            }
            currentLevel = FileHandler.Save(temp);
        }
        public void Load(string name)
        {
            currentLevel = name;
            BuildLevel(FileHandler.Load(currentLevel));
        }
        public Texture2D LoadBackground(string name)
        {
            return Content.Load<Texture2D>(@"BackGrounds\" + name);
        }
        public Texture2D LoadTexture(string name)
        {
            return Content.Load<Texture2D>("Textures//" + name);
        }
        public void LoadTextures()
        {
            this.Visible = false;
            Textures.Add(TileName.BigBoulder, LoadTextureGP("BoulderBig"));
            Textures.Add(TileName.MediumBoulder, LoadTextureGP("BoulderMedium"));
            Textures.Add(TileName.SmallBoulder, LoadTextureGP("BoulderSmall"));
            Textures.Add(TileName.CrossPlank, LoadTextureGP("CrossPlank"));
            Textures.Add(TileName.Fan, LoadTexture("FanSprite"));
            Textures.Add(TileName.Lava, LoadTexture("LavaSprite"));
            Textures.Add(TileName.Magnet, LoadTexture("Magnet"));
            Textures.Add(TileName.Oil, LoadTexture("OilSprite"));
            Textures.Add(TileName.Plank, LoadTextureGP("Plank"));
            Textures.Add(TileName.Plastic, loadMaterial("Plastic"));
            Textures.Add(TileName.Quarterpipe, LoadTextureGP("QuarterPipe"));
            Textures.Add(TileName.Quicksand, LoadTexture("QuicksandSprite"));
            Textures.Add(TileName.Rubber, loadMaterial("Rubber"));
            Textures.Add(TileName.Spike, LoadTextureGP("Spike"));
            Textures.Add(TileName.SpikeyRobin, LoadTextureGP("SpikeyRobin"));
            Textures.Add(TileName.Steel, loadMaterial("Steel"));
            Textures.Add(TileName.Stone, loadMaterial("Stone"));
            Textures.Add(TileName.Vacuum, LoadTexture("Vacuum"));
            Textures.Add(TileName.Water, LoadTexture("WaterSprite"));
            Textures.Add(TileName.Wood, loadMaterial("Wood"));
            Textures.Add(TileName.AntiClockwiseArrow, LoadTexture("AntiClockwiseArrow"));
            Textures.Add(TileName.ClockwiseArrow, LoadTexture("ClockwiseArrow"));
            Textures.Add(TileName.Start,LoadTexture("Start"));
            Textures.Add(TileName.Fin, LoadTexture("Fin"));
            Textures.Add(TileName.TeleportIn, LoadTexture("TPinSprite"));
            Textures.Add(TileName.TeleportOut, LoadTexture("TPoutSprite"));
            Textures.Add(TileName.GravityShift, LoadTexture("GravityShift"));

            for (int i = 1; i <= 11; i++)
            {
                Backgrounds.Add(LoadBackground("background" + i));
            }

            

            this.Visible = true;
        }
        public Texture2D LoadTextureGP(string name)
        {
            return Content.Load<Texture2D>("TextureGamePieces//" + name);
        }
        public Texture2D loadMaterial(string name)
        {
            return Content.Load<Texture2D>("Materials//" + name);
        }
        public static void loadNext()
        {
            if (PhysicsGame.gamestate == GameState.Edit && !Singleton.editorMode)
            {
                Singleton.ToggleEditorMode();
            }
            else
            {
                try
                {
                    string next = "Level" + (Convert.ToInt16(currentLevel.Remove(0, 5)) + 1).ToString();
                    Singleton.Load(next);
                }
                catch
                {
                    Singleton.Load("Level0");
                }
            }
        }
        public static void Reload()
        {
            AudioMan.PlayCue("Fail");
            if (PhysicsGame.gamestate != GameState.Edit)
            {
                EffectTile.instances = 0;
                EffectTile.instanceWithAccess = int.MaxValue;
                Singleton.Load(currentLevel);
            }
        }
        public void Reset()
        {
                world.Clear();
                Gravity = new Vector2(0,4);
                physicsGame.Background = Backgrounds[0];
                levelSize = Vector2.Zero;
                if (border != null)
                {
                    Components.Remove(border);
                    border.Dispose();
                }
                SaveLineCounter = 0;
                foreach (GamePiece x in gamePieces)
                    Components.Remove(x);
                gamePieces.Clear();
                stringList.Clear();
                if (editorMode)
                    BuildBorder = true;
        }
        public void ToggleEditorMode()
        {
            if (editorMode)
            {
                UNITGP = new CircleUnit(physicsGame, world, materials["Wood"], StartPos, 0, 22, 0.5f, SaveLineCounter, "Player");
                GamePiece._ScreenOffset = StartPos;
                UNITGP.Initialize();
                gamePieces.Add(UNITGP);
                Components.Add(UNITGP);
            }
            else if(PhysicsGame.gamestate == GameState.Edit)
            {
                Gravity = gravity;
                world.RemoveBody(UNITGP.body);
                gamePieces.Remove(UNITGP);
                Components.Remove(UNITGP);
            }
            editorMode = !editorMode;
        }
        public void UpdateCurrentSelectedTile()
        {
            if (!WaitingForTilePlacement)
            {
                switch (GPindex)
                {
                    case 0: currentSelectedTile = TileName.BigBoulder;
                        break;
                    case 1: currentSelectedTile = TileName.MediumBoulder;
                        break;
                    case 2: currentSelectedTile = TileName.SmallBoulder;
                        break;
                    case 3: currentSelectedTile = TileName.Plastic;
                        break;
                    case 4: currentSelectedTile = TileName.Rubber;
                        break;
                    case 5: currentSelectedTile = TileName.Steel;
                        break;
                    case 6: currentSelectedTile = TileName.Wood;
                        break;
                    case 7: currentSelectedTile = TileName.Stone;
                        break;
                    case 8: currentSelectedTile = TileName.Water;
                        break;
                    case 9: currentSelectedTile = TileName.Lava;
                        break;
                    case 10: currentSelectedTile = TileName.Oil;
                        break;
                    case 11: currentSelectedTile = TileName.Quicksand;
                        break;
                    case 12: currentSelectedTile = TileName.Fan;
                        break;
                    case 13: currentSelectedTile = TileName.Magnet;
                        break;
                    case 14: currentSelectedTile = TileName.Vacuum;
                        break;
                    case 15: currentSelectedTile = TileName.CrossPlank;
                        break;
                    case 16: currentSelectedTile = TileName.Plank;
                        break;
                    case 17: currentSelectedTile = TileName.SpikeyRobin;
                        break;
                    case 18: currentSelectedTile = TileName.Quarterpipe;
                        break;
                    case 19: currentSelectedTile = TileName.Spike;
                        break;
                    case 20: currentSelectedTile = TileName.Start;
                        break;
                    case 21: currentSelectedTile = TileName.Fin;
                        break;
                    case 22: currentSelectedTile = TileName.TeleportIn;
                        break;
                    case 23: currentSelectedTile = TileName.GravityShift;
                        break;
                    default:
                        break;
                }
            }

        }
        #endregion

        #region Auto Call Functions
        public override void Draw(GameTime gameTime)
        {
            if (editorMode&&!BuildBorder)
            {
                DrawDecoyImage();
                DrawStartPos();
            }
            else if (BuildBorder)
            {
                sb.DrawString(font, "Left/Right Width, Up/Down Hight", new Vector2(viewport.X - viewport.X * 0.6f, 50), Color.White);
                sb.DrawString(font, "Width = "+levelSize.X, new Vector2(viewport.X - viewport.X * 0.6f, 75), Color.White);
                sb.DrawString(font, "Height = "+levelSize.Y, new Vector2(viewport.X - viewport.X * 0.6f, 100), Color.White);
            }
        }
        public override void Update(GameTime gameTime)
        {
            HandleInput();
            if (BuildBorder)
                BuildBorderLoop();
            UpdateCurrentSelectedTile();
            rotation += InputMan.scrollDirection() * 5;
            rpm += InputMan.scrollDirection() * 2;
            try
            {
                if (world.BodyList != null)
                    world.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalSeconds, (1f / 30f)));
            }
            catch { }
            float SecondsPassed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            HandleCamera();
            if (physicsGame.viewDebug)
                updateDebug(gameTime);
            base.Update(gameTime);
        }
        #endregion

        #region CameraFunctions
        public void HandleCamera()
        {
            if (EditorMode)
                HandleEditorCamera();
            else
                HandleOnUserCamera();
        }
        public void HandleOnUserCamera()
        {
            if (PlayerVelocity.Y < 0)
                if (PlayerPosition.Y <= -Yoffset + ScreenMoveLimitSize.Y)
                    Yoffset = -(PlayerPosition.Y - ScreenMoveLimitSize.Y);

            if (PlayerVelocity.X < 0)
                if (PlayerPosition.X <= -Xoffset + ScreenMoveLimitSize.X)
                    Xoffset = -(PlayerPosition.X - ScreenMoveLimitSize.X);

            if (PlayerVelocity.Y > 0)
                if (PlayerPosition.Y >= -Yoffset + viewport.Y - ScreenMoveLimitSize.Y)
                    Yoffset = -(PlayerPosition.Y + ScreenMoveLimitSize.Y - viewport.Y);

            if (PlayerVelocity.X > 0)
                if (PlayerPosition.X >= -Xoffset + viewport.X - ScreenMoveLimitSize.X)
                    Xoffset = -(PlayerPosition.X + ScreenMoveLimitSize.X - viewport.X);
  
            if (-Xoffset >= levelSize.X - viewport.X)
            {
                Xoffset = -(levelSize.X - viewport.X);
            }

            if (-Yoffset >= levelSize.Y - viewport.Y)
            {
                Yoffset = -(levelSize.Y - viewport.Y);
            }

            if (Xoffset >= 0)
                Xoffset = 0;
            if (Yoffset >= 0)
                Yoffset = 0;

          

        }
        public void HandleEditorCamera()
        {
            int movespeed = 5;

            if(InputMan.IsKeyDown(Keys.NumPad4))
                Xoffset += movespeed;
            if (InputMan.IsKeyDown(Keys.NumPad6))
                Xoffset -= movespeed;
            if (InputMan.IsKeyDown(Keys.NumPad2))
                Yoffset -= movespeed;
            if (InputMan.IsKeyDown(Keys.NumPad8))
                Yoffset += movespeed;
              if (-Xoffset >= levelSize.X - viewport.X)
            {
                Xoffset = -(levelSize.X - viewport.X);
            }

            if (-Yoffset >= levelSize.Y - viewport.Y)
            {
                Yoffset = -(levelSize.Y - viewport.Y);
            }
            if (Xoffset >= 0)
                Xoffset = 0;
            if (Yoffset >= 0)
                Yoffset = 0;

          
        }
        #endregion

        #region Enums
        public enum TileName
        {
            Stone, Wood, Steel, Plastic, Rubber, Lava,
            Oil, Water, Fan, Quicksand, 
            Magnet, SpikeyRobin, CrossPlank,
            Plank, Spike, Vacuum, Quarterpipe, BigBoulder, MediumBoulder, SmallBoulder, TeleportIn, TeleportOut, ClockwiseArrow, AntiClockwiseArrow, Start, Fin, GravityShift,
        }
        #endregion

        #region Debug goed
        DebugViewXNA DVXNA;
        Camera2D Cam;
        private void DebugStuff()
        {
            DVXNA = new DebugViewXNA(world);
            DVXNA.LoadContent(physicsGame.graphics.GraphicsDevice, Content);
            Cam = new Camera2D(physicsGame.graphics.GraphicsDevice);
        }
        private void updateDebug(GameTime gameTime)
        {
            Matrix projection = Cam.SimProjection;
            Matrix view = Cam.SimView;
            DVXNA.RenderDebugData(ref projection, ref view);
            UpdateDebugCam(gameTime);
        }
        public void UpdateDebugCam(GameTime gameTime)
        {
            Vector2 camMove = Vector2.Zero;

            if (InputMan.IsKeyDown(Keys.Up))
            {
                camMove.Y -= 2.5f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                camMove.Y += 2.5f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                camMove.X -= 2.5f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                camMove.X += 2.5f * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.PageUp))
            {
                Cam.Zoom += 5f * (float)gameTime.ElapsedGameTime.TotalSeconds * Cam.Zoom / 20f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.PageDown))
            {
                Cam.Zoom -= 5f * (float)gameTime.ElapsedGameTime.TotalSeconds * Cam.Zoom / 20f;
            }
            if (camMove != Vector2.Zero)
            {
                Cam.MoveCamera(camMove);
            }

            Cam.Update(gameTime);
        }
        #endregion


        public void PlayMode()
        {
            EditorMode = false;
            BuildBorder = false;
            Load("Level0");
        }
        public void PlayMode(string LevelToLoad)
        {
            EditorMode = false;
            BuildBorder = false;
            Load(LevelToLoad);
        }
        public void EditMode()
        {
            Reset();
            editorMode = true;
            world.Clear();
            BuildBorder = true;

        }
    }
    
}